<template>
  <div class="slideChart">
    <div id="rainfall" class="echarts"></div>
  </div>
</template>

<script>
export default {
  name: "slideChart",
  components: {},
  props: {},
  data () {
    return {
      chartData: {
        date: [],
        price: []
      }
    }
  },
  computed: {},
  watch: {},
  created () {},
  mounted () {
    this.mockData();
    this.$nextTick().then(() => {
      this.getRainfall();
    })
  },
  methods: {
    // 获取数据
    mockData() {
      let base = + new Date('2018/1/1')
      let oneDay = 24 * 3600 * 1000
      let date = []
      let data = []
      let len = Math.ceil((+new Date() - base) / oneDay)
      for (let i = 0; i < len; i++) {
        let item = new Date(base)
        base += oneDay
        date.push(
          item.getFullYear() + '/' +
          (item.getMonth() + 1) + '/' +
          item.getDate()
        )
        data.push(Math.floor(Math.random() * (1000 - 500 + 1) + 500))
      }
      this.chartData.date = date
      this.chartData.price = data
    },
    // 雨量流量关系图
    getRainfall(){
      let chartDom = document.getElementById("rainfall");
      let chart = this.$echarts.init(chartDom);
      let option = {
        title: {
          text: '商品价格变动图',
          left: 'center'
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross',
            label: {
              background: '#6a7985'
            }
          }
        },
        grid: {
          left: '20',
          right: '20',
          bottom: '30',
          containLabel: true
        },
        xAxis: [
          {
            type: 'category',
            boundaryGap: false,
            data: this.chartData.date
          }
        ],
        yAxis: [
          {
            type: 'value',
            boundaryGap: [0, '100%']
          }
        ],
        dataZoom: [
          {
            type: 'inside',
            start: 0,
            end: 10
          },
          {
            start: 0,
            end: 10,
            handleSize: '80%',
            handleStyle: {
              color: '#fff',
              shadowBlur: 3,
              shadowColor: 'rgba(0, 0, 0, 0.6)',
              shadowOffsetX: 2,
              shadowOffsetY: 2
            },
            bottom: 0
          }
        ],
        series: [
          {
            name: '商品价格',
            type: 'line',
            areaStyle: {
              color: '#55a8fd',
              opacity: 0.3
            },
            itemStyle: {
              color: '#55a8fd'
            },
            lineStyle: {
              color: '#55a8fd'
            },
            smooth: true,
            data: this.chartData.price,
            animationDuration: 2800,
            animationEasing: 'quadraticOut'
          }
        ]
      };
      chart.setOption(option);
    },
  },
}
</script>

<style lang="less" scoped>
.slideChart {
  width: 100%;
  height: 100%;
  background-color: #fff;
  padding: 20px;
  .echarts{
    width: 100%;
    height: 100%;
  }
}
</style>